# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# gtk3 support is still experimental
# It can be tested via _use_gtk3=yes
_use_gtk3=no
if [ "${_use_gtk3}" = "yes" ]; then
  _gtksuff=3
fi

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.92.5
pkgrel=3
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
url="https://inkscape.sourceforge.io/"
license=("GPL" "LGPL")
install=inkscape-${CARCH}.install
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "intltool")
depends=(
          "${MINGW_PACKAGE_PREFIX}-aspell"
          "${MINGW_PACKAGE_PREFIX}-gc"
          "${MINGW_PACKAGE_PREFIX}-ghostscript"
          "${MINGW_PACKAGE_PREFIX}-gsl"
          "${MINGW_PACKAGE_PREFIX}-gtkmm${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-gtkspell${_gtksuff}"
          "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
          "${MINGW_PACKAGE_PREFIX}-imagemagick"
          "${MINGW_PACKAGE_PREFIX}-lcms2"
          "${MINGW_PACKAGE_PREFIX}-libcdr"
          "${MINGW_PACKAGE_PREFIX}-libvisio"
          "${MINGW_PACKAGE_PREFIX}-libxml2"
          "${MINGW_PACKAGE_PREFIX}-libxslt"
          "${MINGW_PACKAGE_PREFIX}-libwpg"
          "${MINGW_PACKAGE_PREFIX}-poppler"
          "${MINGW_PACKAGE_PREFIX}-popt"
          "${MINGW_PACKAGE_PREFIX}-potrace"
          "${MINGW_PACKAGE_PREFIX}-python"
          $([[ "${_use_gtk3}" == "yes" ]] && echo \
            "${MINGW_PACKAGE_PREFIX}-gdl" ) \
        )
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            #"${MINGW_PACKAGE_PREFIX}-texlive-core: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-python-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
options=('staticlibs' 'strip')
source=(https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.bz2
        inkscape-0.48.4-headers-fix.patch
        inkscape-0.48.4-python.patch
        inkscape-0.92.0-win32-relocatable.patch
        inkscape-0.48.4-python-extensions-location.patch
        inkscape-0.91-simulaneous-intltool-gettext.patch
        inkscape-0.92.3-abs-overloaded.patch
        inkscape-0.92.5-skip-install-mingw.patch
        inkscape-0.92.5-fix-warning.patch)
sha256sums=('81ee7f69e2bd64a97343d8395a4a7a7905b21b861566ed5e5d9179178d519a0b'
            'e578fbef476704a42522ec5298f0202d2e600c2e03a98dba2c62d4ee1e53b6b5'
            'c676771eec2fe4249c4b483d94fd09955547e04656b2e10a60e06ae40bc2d805'
            'e9daa7b7e77cfbc59a56a19e8315414d6861875f6274fd48ac61da1411290b74'
            '12cc284239caa6bed2474afc7ea7fecefad55fb7d5679b124220ebbd6f276ee1'
            'ea625d1d7260d32866e4e61946fa02793b1762f66dc223364dbcc5e628bb5bfe'
            '833980a8bd2c67be66a8d7c211c7996984728372a2c55ec71e1f93e2fb2343c5'
            '71c5b0c08c88a20a7d7ce73771ff41e8f1d4a25b0ceeae359cbda22ab9aa1cfc'
            'dc3c347919239e1eb1c5b256b8015b70639e6f9c2f8540b079b7609d26defdb8')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/inkscape-0.48.4-headers-fix.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python.patch
  patch -p1 -i ${srcdir}/inkscape-0.92.0-win32-relocatable.patch
  patch -p1 -i ${srcdir}/inkscape-0.48.4-python-extensions-location.patch
  #patch -p1 -i ${srcdir}/inkscape-0.91-simulaneous-intltool-gettext.patch
  
  patch -p1 -i ${srcdir}/inkscape-0.92.3-abs-overloaded.patch
  patch -p1 -i ${srcdir}/inkscape-0.92.5-skip-install-mingw.patch
  patch -p1 -i ${srcdir}/inkscape-0.92.5-fix-warning.patch
}

build() {
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir -p build-${CARCH} && cd build-${CARCH}

  local -a extra_config
  
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  if [ "${_use_gtk3}" = "yes" ]; then
    extra_config+=(-DWITH_GTK3_EXPERIMENTAL=ON)
  fi
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -Wno-dev \
    -DCMAKE_AR=${MINGW_PREFIX}/bin/ar \
    -DWITH_IMAGE_MAGICK=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${CARCH}
  make DESTDIR="${pkgdir}" install
}
